import router from '@/router/index'
import {useCommonStore} from "@/store";
import {ElMessage} from "element-plus";
import routers from "@/router/index";

const WhiteList = ['/login', '/home', '/home/other', '/enroll', '/home/commodity-detail',
    '/reset-password', '/home/commodity-search', '/home/index', '/home/square'
    , '/home/second']//路由白名单
router.beforeEach((to, from, next) => {
    const store = useCommonStore()
    document.documentElement.scrollTop = 0;
    if (store.isLogin && to.path === '/login') {
        //如果已经登录，还在访问登录页，跳转首页
        next('/home')
    }
    if (WhiteList.indexOf(to.path) === -1) {
        console.log("路由", to.path)
        //    不在白名单里面
        if (store.isLogin) {
            //如果已经登录，放行
            next()
        } else {
            //否则跳转至登录页
            ElMessage.error("请登录！")
            next('/login')
        }
    } else {
        //在白名单里面
        next()
    }
})

//跳转页面后，自动滚动到顶部
// router.beforeEach((to, from, next) => {
//     // chrome
//     document.body.scrollTop = 0;
//     // firefox
//     document.documentElement.scrollTop = 0;
//     // safari
//     window.pageYOffset = 0;
//     next();
// })
